Hey there.
Welcome back.
Let's go ahead and add some footsteps to our player.
This is going to be virtually the exact same as what we've done in our previous project.
So you can actually just copy all of the code and put it in this project.
But I'm still going to go through and script the whole system for creating footsteps.
So we can go ahead and create a new local script inside of starter character scripts.
And this is going to be our footsteps handler.
And we're going to go ahead and use the module script inside of Replicated storage, which is our footstep
module, to go ahead and get random sound IDs based on whatever material we are walking on.
I'm going to be using the same template as before, but we're not going to be having this be a service,
but instead we just want the nice little comment separations to keep our code nice and organized.
And all we're going to need is replicated storage to get that module script.
So first we'll make a reference to that module.
I'll just call it footstep module.
And we'll require from replicated storage dot modules dot footstep module.
We'll get the players character which is literally just script dot parent.
We will get the humanoid which is going to be equal to the character, and we'll wait for the humanoid
instance.
We'll also get the route part and that's character.
And we'll wait for the humanoid route part as well, just in case.
We're also going to get the default running sound or walking sound.
We'll call it running sound.
And that's in the root part.
And it's called running.
And we want to just basically update that value on the volume for this running sound equal to zero.
Um, some other things we want to keep track of is the player's original walking speed.
So we'll call this variable walk speed equal to humanoid walk speed.
We'll get the player's current speed and a variable that'll be equal to zero for now.
And then we can have another variable store, a value that's going to be our cooldown or the time between
footsteps.
By default we'll set it to 0.35, but if we detect that the player's walk speed increases or decreases,
we'll update that value accordingly.
And then once we do that, we can just literally set the running student volume equal to zero.
And we'll actually do that inside of our main section here.
But now that we've got all that updated, we can go ahead and create some functions we'll need.
For example, we can have a function for playing a footstep sound.
And we'll pass a sound ID to this function.
We'll also want to have a function to check whether or not our player is moving.
So we'll call it check movement.
And then we'll need a function to get the, uh, sound ID based on whatever material we are standing
on.
So we can call this get sound based on material.
And then what we could do is I'm going to rename this here to be our handler section, because we're
going to be listening to some events.
For example, we want to listen for when the property of our walk speed changes on our humanoid.
And when it does, we want to go ahead and check to see if the humanoid dot walk speed is greater than
the walk speed in our variable.
And if it is, we want to update the cooldown to be less.
I'll say something like 0.25.
If the walk speed of our humanoid becomes less than the default recorded walk speed, then we're going
to set cooldown to be longer.
Otherwise, the default for our cool down again is going to be 0.35 seconds.
Another thing we want to listen to is the running event for our humanoid.
And this will pass to us the current speed of our player, and then we can just go ahead and set the
current speed equal to that running speed.
And now inside of the main section, we'll have a while loop that will constantly update the movement
of our player, and if we're moving, then we'll go ahead and play a footstep sound.
Now we're going to have to create a footstep sound inside of our player.
So what I'm going to do is I'm going to create a new sound instance.
I'm going to call it footstep sound.
That's going to be equal to instance dot new sound.
We're going to set the name of the sound equal to footsteps.
We're going to set the volume to like 0.15.
And then we'll update the roll off max distance equal to like 30 studs.
The footstep sound roll off mode is going to be equal to enum dot roll off mode dot linear.
Uh or maybe I think it's inverse.
Yeah.
We'll do inverse tapered.
And then the parent of our footstep sound of course is going to be equal to our root part.
Now we can go ahead and do is I'll use task dot defer to spawn a new function.
And inside of here we'll have our while loop.
And what this while loop is going to do is check whether or not our player is moving.
So we'll have uh our check movement function return a boolean.
So if we check our movement and it's true, then we can go ahead and play a footstep sound.
And that's where we would call our play footstep sound function and pass the sound ID.
And since we'll get the sound ID based on whatever material we're standing on, then we can literally
call get sound based on material within the parentheses or the call of our play footsteps down.
So this will play our footstep, and then we'll go ahead and yield inside of our loop for that.
Uh.
Cool down.
And now all we need to do is go ahead and fill out these three functions here.
So for example, to check the movement of the humanoid, first we want to go ahead and get the last
material that our humanoid was standing on.
So it's going to be equal to humanoid dot floor material.
And actually I think we forgot to make a variable for this.
So I'll just make it up here.
We'll call this last material and set it equal to nil.
So we'll get the last material that the humanoid was standing on.
And we want to make sure that our humanoids walk speed is greater than zero.
We also want to make sure the humanoid and we get their move direction.
Actually, let me go ahead and denote the humanoid as a humanoid because it's not showing us that autofill
stuff.
So if the humanoid dot move direction.
Dot magnitude is greater than zero, and we validate that our current speed is greater than zero.
And we want to make sure that we're actually standing on something.
So we want to make sure that last material is not equal to nil.
Or we want to make sure that the last material we are standing on is not air.
So we'll make sure that's not equal to the enum dot material of air.
If all of this matches up, well, then we can go ahead and return true.
Otherwise we'll just return false.
So if we're moving, that means we'll grab a sound based on what material we're standing on.
So that means inside of our footstep module there's a function called get table from material.
So it'll return to us a table of sound IDs based on a material.
And that's going to be equal to whatever last material we were standing on.
So we'll create a variable called sound table.
And then we can go ahead and get a random sound from the sound table.
So we'll create a variable called random sound.
And that's going to be equal to inside of the footstep module there's a function called get random sound.
And we pass a table here.
So that's going to be our sound table.
And then we can go ahead and return this random sound.
Then we pass this random sound to our play footstep sound function.
So when we do that, we want to go ahead and do is we want to get the footstep sound in our player.
So we'll create a variable called footstep sound and that's equal to root part.
Find first child footsteps.
If for some reason this does not exist, then we're just going to return.
But I don't anticipate this not existing.
But for some reason something could happen.
You never know in the future.
But once we do that, we can go ahead and update the footstep sound dot sound ID equal to the sound
ID that was passed to this function.
And then we can just play the footstep sound just like that.
So if we go and playtest our game and we get that local script copied to our player's character if we
start walking around.
As you can see, we are playing footsteps based on whatever material we are standing on.
And then if I go ahead and increase the speed of my player.
So if I go and update in my player's character the walk speed, let's say I set the walk speed to like
24 and then I go back to my client.
As you can see, we updated that cooldown value and now it is playing the footsteps much faster.
And if I go back and I update the value to be something lower, for example, maybe maybe we'll set
it to like 12.
And then we go back.
As you can see, the footsteps are playing much slower now.
And just like that, we have successfully added footsteps to our game.
Not much to it.